$(function () {
    $("#jqGrid").jqGrid({
        url: '../clbx/list',
        datatype: "json",
        colModel: [			
			{ label: 'clbxId', name: 'clbxId', index: 'clbx_id', width: 50, key: true ,hidden:true},
            { label: '流程状态', name: 'pistatus', index: 'pistatus', width: 130 ,formatter:pistatus},
            { label: '姓名', name: 'userdesc', index: 'userdesc', width: 80 },
            { label: '结算点', name: 'settlementpoint', index: 'settlementpoint', width: 130 ,formatter:getDictInfo},
            { label: '附件id', name: 'fileId', index: 'file_id', width: 80 ,hidden:true},
			{ label: '报销人群', name: 'bxrqs', index: 'bxrqs', width: 80 },
			{ label: '报销人群id', name: 'bxrqids', index: 'bxrqids', width: 80 ,hidden:true},
			{ label: '部门id', name: 'deptid', index: 'deptid', width: 80 ,hidden:true },
			{ label: '部门名称', name: 'deptName', index: 'dept_name', width: 80 },
			{ label: '职级', name: 'zj', index: 'zj', width: 80 }, 			
			{ label: '出差事由', name: 'ccsy', index: 'ccsy', width: 80 }, 			
			{ label: '计划天数', name: 'jhts', index: 'jhts', width: 80 }, 			
			{ label: '实际天数', name: 'sjts', index: 'sjts', width: 80 }, 			
			{ label: '交通费用小计', name: 'jtfyxj', index: 'jtfyxj', width: 80 }, 			
			{ label: '其他费用小计', name: 'qtfyxj', index: 'qtfyxj', width: 80 }, 			
			{ label: '备注', name: 'bz', index: 'bz', width: 80 },
            { label: '部门', name: 'dept', index: 'dept', width: 80 },
            { label: '岗位', name: 'post', index: 'post', width: 80 ,formatter:getDictInfo},
			{ label: '预支金额', name: 'yzje', index: 'yzje', width: 80 }, 			
			{ label: '核销金额', name: 'hxje', index: 'hxje', width: 80 }, 			
			{ label: '退补金额', name: 'tbje', index: 'tbje', width: 80 },
			{ label: '住宿包干报销标准', name: 'zsbgbxBz', index: 'zsbgbx_bz', width: 80 },
			{ label: '计算天数', name: 'zsbgbxJsts', index: 'zsbgbx_jsts', width: 80 }, 			
			{ label: '核报金额', name: 'zsbgbxHbje', index: 'zsbgbx_hbje', width: 80 },
			{ label: '住宿限额报销标准', name: 'zsxebxBz', index: 'zsxebx_bz', width: 80 },
			{ label: '计算天数', name: 'zsxebxJsts', index: 'zsxebx_jsts', width: 80 }, 			
			{ label: '核报金额', name: 'zsxebxHbje', index: 'zsxebx_hbje', width: 80 },
			{ label: '伙食补助费标准', name: 'hsbzfBz', index: 'hsbzf_bz', width: 80 },
			{ label: '计算天数', name: 'hsbzfJsts', index: 'hsbzf_jsts', width: 80 }, 			
			{ label: '核报金额', name: 'hsbzfHbje', index: 'hsbzf_hbje', width: 80 },
			{ label: '市内交通费标准', name: 'snjtfBz', index: 'snjtf_bz', width: 80 },
			{ label: '计算天数', name: 'snjtfJsts', index: 'snjtf_jsts', width: 80 }, 			
			{ label: '核报金额', name: 'snjtfHbje', index: 'snjtf_hbje', width: 80 },
			{ label: '车、船补助费标准', name: 'ccbzfBz', index: 'ccbzf_bz', width: 80 },
			{ label: '计算天数', name: 'ccbzfJsts', index: 'ccbzf_jsts', width: 80 }, 			
			{ label: '核报金额', name: 'ccbzfHbje', index: 'ccbzf_hbje', width: 80 },
			{ label: '其他杂支标准', name: 'qtzzBz', index: 'qtzz_bz', width: 80 },
			{ label: '计算天数', name: 'qtzzJsts', index: 'qtzz_jsts', width: 80 }, 			
			{ label: '核报金额', name: 'qtzzHbje', index: 'qtzz_hbje', width: 80 }, 			
			{ label: '提交时间', name: 'tjDate', index: 'tj_date', width: 80 }, 			
			{ label: '提交人', name: 'tjr', index: 'tjr', width: 80 }, 			
			{ label: '提交人id', name: 'tjrId', index: 'tjr_id', width: 80,hidden:true },
            { label: '审批/抄送人', name: 'nextUser', index: 'next_user', width: 90 },
            { label: '流程ID', name: 'piid', index: 'piid', width: 80,hidden:true }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        //字段太多自动加载横轴滚动条
        shrinkToFit:false,
        autoScroll: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    vm.getDetinfo();//获取明细列表
    vm.getuserlist();//获取报销员工
    var r = utils.getDictList("setlever");
    vm.setlever = r.setlever;
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
        showtable:false,
		title: null,
		clbx: {},
        clbxDetList:[],
        q:{
            clbxid:null
        },
        quser:{
            userdesc:null
        },
        setlever:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
            vm.showtable = false;
			vm.title = "新增";
			vm.clbx = {
                bxrqs:"",
                deptName:"",
                peopleno:0
            };
            vm.clbxDetList=[];
            vm.clbxDetList.push({
                cfd:"",
                ddd: "",
                cfDate:"",
                jtlb:"",
                je:null
            });
		},
		update: function (event) {
			var clbxId = getSelectedRow();
			if(clbxId == null){
				return ;
			}
            var rowData=$("#jqGrid").jqGrid("getRowData",clbxId);
            if(rowData.piid!=''&&rowData.piid!=null&&rowData.piid!='0')
            {
                alert("数据已提交，不能修改");
                return ;
            }
			vm.showList = false;
            vm.showtable = false;
            vm.title = "修改";
            
            vm.getInfo(clbxId);
		},
		saveOrUpdate: function (event) {
            console.log(vm.clbxDetList);
            if(vm.clbxDetList==undefined||vm.clbxDetList.length==0){//最少要有一行明细数据
                alert("最少要有一行明细数据！");
                return;
            }
            var params={"clbxDetList":vm.clbxDetList,
                "clbx":vm.clbx
            };
			var url = vm.clbx.clbxId == null ? "../clbx/save" : "../clbx/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(params),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var clbxIds = getSelectedRows();
			if(clbxIds == null){
				return ;
			}
            var len=clbxIds.length;
            for(var i=0;i<len;i++){
                var rowData=$("#jqGrid").jqGrid("getRowData",clbxIds[i]);
                if(rowData.piid!=''&&rowData.piid!=null&&rowData.piid!='0')
                {
                    alert("数据已提交，不能删除");
                    return ;
                }
            }

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../clbx/delete",
				    contentType: "application/json",
				    data: JSON.stringify(clbxIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(clbxId){
			$.get("../clbx/info/"+clbxId, function(r){
                vm.clbx = r.map.clbx;
                vm.clbxDetList = r.map.clbxDetList;
            });
		},
		reload: function (event) {
			vm.showList = true;
            vm.showtable = false;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
        addCertificateItem: function(){
            vm.clbxDetList.push({
                cfd:"",
                ddd: "",
                cfDate:"",
                jtlb:"",
                je:null
            });
        },
        delCertificateItem: function(index){
        	//console.log(index);
            vm.clbxDetList.splice(index,1);
        },
        getDetinfo: function(){
            $("#jqGridTable").jqGrid({
                url: '../clbxdet/list',
                datatype: "local",
                colModel: [
                    { label: 'bxdetId', name: 'bxdetId', index: 'bxdet_id', width: 50, key: true ,hidden:true},
                    { label: '差旅报销id', name: 'clbxId', index: 'clbx_id', width: 80 ,hidden:true},
                    { label: '出发地', name: 'cfd', index: 'cfd', width: 80 },
                    { label: '抵达地', name: 'ddd', index: 'ddd', width: 80 },
                    { label: '出发日期', name: 'cfDate', index: 'cf_date', width: 80 },
                    { label: '交通类别', name: 'jtlb', index: 'jtlb', width: 80 },
                    { label: '金额', name: 'je', index: 'je', width: 80 }
                ],
                viewrecords: true,
                height: 300,
                width: 500,
                rowNum: 5,
                rowList : [10,30,50],
                rownumbers: true,
                autowidth:false,
                //字段太多自动加载横轴滚动条
				/*  shrinkToFit:false,
				 autoScroll: false,
				 multiselect:false,*///只允许单选
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                }
            });
        },
        reloadDetinfo: function (event) {
            var page = $("#jqGridTable").jqGrid('getGridParam','page');
            $("#jqGridTable").jqGrid('setGridParam',{
                datatype:'json',
                postData : {clbxid : vm.q.clbxid},
                page:page
            }).trigger("reloadGrid");
            utils.openDialogView('报销明细',$("#window"),'550px','400px',1);
        },
        querydet:function(){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.q.clbxid=id;
            vm.reloadDetinfo();
        },


        submit: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            var piid=rowData.piid;
            if(piid!=0&&piid!=null&&piid!=""){
                alert("数据已提交，不能重复提交！");
                return;
            }
            var url = "../clbx/submit";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(id),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },

        processInt: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.showtable = true;
            vm.title = "审批";

            vm.getInfo(id)
        },

        process: function (event) {
            var url = "../clbx/process";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.clbx),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reject: function (event) {
            var url = "../clbx/reject";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.clbx),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        popHistory: function(){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            var piid=rowData.piid;
            if(piid==0||piid==null||piid==""){
                alert("没有流程！");
                return;
            }
            utils.popHistory(piid);
            var page = $("#popHistoryGrid").jqGrid('getGridParam','page');
            $("#popHistoryGrid").jqGrid('setGridParam',{
                postData:{
                    'piid': piid,
                },
                page:page
            }).trigger("reloadGrid");
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "历史记录",
                area: ['725px', '400px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#popHistory"),
                /*  btn: ['确定', '取消'],
                 btn1: function (index) {
                 layer.close(index);
                 }*/
            });
        },
        queryfile:function(){
            var Id = getSelectedRow();
            if(Id == null){
                return ;
            }
            var rowData = $("#jqGrid").jqGrid("getRowData", Id);
            utils.openDialogView('上传附件',"../sys/upload.html?fileId="+rowData.fileId,"900px","500px",2);
        },
        selectuser:function(){
            vm.queryuser();
            utils.openDialogView('选择员工',$("#windowuser"),"700px","380px",1);
        },
        getuserlist:function(){
            $("#jqGridTableuser").jqGrid({
                url: '../sys/user/list',
                datatype: "local",
                colModel: [
                    { label: '用户ID', name: 'userId', index: "user_id", width: 45, key: true ,hidden:true},
                    { label: '手机号', name: 'mobile', width: 150 },
                    { label: '姓名', name: 'userdesc', width: 100 },
                    { label: '部门', name: 'dept', width: 350 },
                    { label: '部门id', name: 'deptNo', width: 100 ,hidden:true}
                ],
                viewrecords: true,
                height: 245,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
              //  pager: "#jqGridPageruser",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                   // $("#jqGridTableuser").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
        },
        reloaduserlist: function (event) {
            var page = $("#jqGridTableuser").jqGrid('getGridParam','page');
            $("#jqGridTableuser").jqGrid('setGridParam',{
                datatype:'json',
                postData:{'userdesc': vm.quser.userdesc},
                page:page
            }).trigger("reloadGrid");

        },
        queryuser: function () {
            vm.reloaduserlist();
        },
        adduser:function(){

            var ids=$("#jqGridTableuser").jqGrid("getGridParam","selarrrow");
            if(ids.length==0){
                alert("请选择一条记录");
                return ;
            }

            for(var i=0;i<ids.length;i++){
                var rowData=$("#jqGridTableuser").jqGrid("getRowData",ids[i]);
                var t = vm.clbx.bxrqs.indexOf(rowData.userdesc);
                if(t>=0){
                    alert("报销人群中已有："+rowData.userdesc);
                    return;
                }
                vm.clbx.peopleno++;
                vm.clbx.bxrqs+="("+vm.clbx.peopleno+")"+rowData.userdesc+";";
                vm.clbx.deptName+="("+vm.clbx.peopleno+")"+rowData.dept+";";
            }
        },
        clearuser:function(){
            vm.clbx.peopleno=0;
            vm.clbx.bxrqs="";
            vm.clbx.deptName="";
        }

    }
});
